package com.example.leetcode.bfs;

import com.example.leetcode.tree.common.TreeNode;
import com.example.leetcode.tree.common.TreeUtils;

import java.util.LinkedList;
import java.util.Queue;

public class gettreedepth {
    public static void main(String[] args) {
        TreeNode root = TreeUtils.generateAtreeByRandomVal(1, 10, 5);
        int depth = getDepth(root);
        int depth1 = getDepth1(root);
        System.out.println(depth);
        System.out.println(depth1);
    }

    private static int getDepth1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return Math.max(getDepth1(root.left), getDepth1(root.right)) + 1;
    }

    private static int getDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            level++;
            for (int i = 0; i < size; i++) {
                TreeNode curNode = queue.poll();
                if (curNode.left != null) {
                    queue.add(curNode.left);
                }
                if (curNode.right != null) {
                    queue.add(curNode.right);
                }
            }
        }
        return level;
    }
}
